﻿using JinfuyuanProject.Entity;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace JinfuyuanProject.Repository
{
    public class ContractRepository:IContractRepository
    {
        private readonly JinfuyuanDbContext _db;
        public ContractRepository(JinfuyuanDbContext db) {
            _db = db;
        }
        #region 基础方法
        public bool Delete(Contract contract)
        {
            _db.Entry(contract).State = System.Data.Entity.EntityState.Deleted;
            return _db.SaveChanges() > 0;
        }

        public IQueryable<Contract> GetList()
        {
            return _db.Contract;
        }

        public Contract GetModel(int Id)
        {
            Contract contract = _db.Contract.Find(Id);
            return contract;
        }

        public int Insert(Contract contract)
        {
            _db.Entry(contract).State = System.Data.Entity.EntityState.Added;
            _db.SaveChanges();
            return contract.Id;
        }

        public bool Update(Contract contract)
        {
            _db.Entry(contract).State = System.Data.Entity.EntityState.Modified;
            return _db.SaveChanges() > 0;
        }
        #endregion
        #region 扩展方法
        // 根据员工ID获取合同信息
        public async Task<List<Contract>> GetContractByEmployeeIdAsync(int employeeId, DateTime startDate, DateTime endDate)
        {
            return await _db.Contract
                .Where(c => c.EmpId == employeeId && c.CreateTime <endDate && c.CreateTime >= startDate)
                .ToListAsync();
        }
        #endregion
    }
}
